﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-04-15 15:55:52
 * 功能：提供移动平台震动接口
**************************************************************************************/

#if UNITY_IOS
using Game.Framework;
using System;
using System.Runtime.InteropServices;

public class IosNative : INativeAPI
{
    #region 原生

    [DllImport("__Internal")]
    private static extern void TriggerVibration_IOS(int feedbackType);

    [DllImport("__Internal")]
    private static extern bool hasSimCard();

    //[DllImport("__Internal")]
    //private static extern void report(string val);

    //[DllImport("__Internal")]
    //private static extern void showIntAd();

    //[DllImport("__Internal")]
    //private static extern void showSelfIntAd();

    //[DllImport("__Internal")]
    //private static extern void showRewardAd();

    //[DllImport("__Internal")]
    //private static extern void showSelfRewardAd();

    [DllImport("__Internal")]
    private static extern void sendBILog(string key, string value);

    [DllImport("__Internal")]
    private static extern void setUserProperty(string value);

    [DllImport("__Internal")]
    private static extern IntPtr getIdfa();

    [DllImport("__Internal")]
    private static extern IntPtr getDeviceId();

    [DllImport("__Internal")]
    private static extern IntPtr getAppToken();

    [DllImport("__Internal")]
    private static extern void SaveImage(string path);

    [DllImport("__Internal")]
    private static extern void SaveVideo(string path);
    #endregion

    /// <summary>触发移动平台震动震动</summary>
    public void TriggerVibration(VibrationType type = VibrationType.Soft)
    {
#if !UNITY_EDITOR
        TriggerVibration_IOS((int)type);
#endif
        LogManager.Log($"触发移动端震动效果：{type}");
    }

    /// <summary>是否有Sim卡</summary>
    public bool HasSimCard()
    {
#if !UNITY_EDITOR
        return hasSimCard();
#else
        return false;
#endif
    }

    /// <summary>给SDK上报数据</summary>
    public void ReportToH5(string data)
    {
#if !UNITY_EDITOR
        //report(data);
#endif
        LogManager.Log($"sdk, data:{data}", "upload");
    }

    /// <summary>显示插屏广告（游戏中所有使用Max接口显示插屏广告的都应该调用这个</summary>
    public void ShowIntAd()
    {
        AdMgr.Instance.ShowInt(GameDataMgr.Instance.TriggerAdType);
        //#if UNITY_EDITOR
        //#else
        //        showIntAd();
        //#endif
        //LogManager.Log($"显示原生插屏广告");
    }

    /// <summary>显示插屏广告（游戏内拉取插屏广告库存不足时调用这个方法）</summary>
    public void ShowSelfntAd()
    {
        //#if !UNITY_EDITOR
        //        showSelfIntAd();
        //#endif
        //        LogManager.Log($"显示自己的插屏广告");
    }

    /// <summary>显示激励广告（游戏中所有使用Max接口显示激励广告的都应该调用这个</summary>
    public void ShowRewardAd()
    {
        AdMgr.Instance.ShowReward(GameDataMgr.Instance.TriggerAdType);
        //#if UNITY_EDITOR
        //#else
        //        showRewardAd();
        //#endif
        //        LogManager.Log($"显示原生激励广告");
    }

    /// <summary>显示激励广告（游戏内拉取插屏广告库存不足时调用这个方法）</summary>
    public void ShowSelfRewardAd()
    {
        //#if !UNITY_EDITOR
        //        showSelfRewardAd();
        //#endif
        //        LogManager.Log($"显示自己的激励广告");
    }

    /// <summary>上报数据（BI打点）</summary>
    public void SendBILog(string key, string value)
    {
        LogManager.Log($"bi, eventName:{key} data:{value}", "upload");
#if !UNITY_EDITOR
        sendBILog(key, value);
#endif
    }

    public void SetUserProperty(string value)
    {
#if !UNITY_EDITOR
        setUserProperty(value);
#endif
        LogManager.Log($"设置UserProperty，{value}");
    }

    public string GetIdfa()
    {
#if !UNITY_EDITOR
        IntPtr ptr = getIdfa();
        string idfa = Marshal.PtrToStringUTF8(ptr);
        LogManager.Log($"idfa:{idfa}");
        return idfa;
#else
        return "";
#endif
    }

    public string GetDeviceId()
    {
#if !UNITY_EDITOR
        IntPtr ptr = getDeviceId();
        string deviceId = Marshal.PtrToStringUTF8(ptr);
        LogManager.Log($"deviceId:{deviceId}");
        return deviceId;
#else
        return GameDataMgr.Instance.Uid.ToString();
#endif
    }

    public string GetAppToken()
    {
#if !UNITY_EDITOR
        IntPtr ptr = getAppToken();
        string appToken = Marshal.PtrToStringUTF8(ptr);
        LogManager.Log($"appToken:{appToken}");
        return appToken;
#else
        return "";
#endif
    }

    public void SaveImageToAlbum(string path)
    {
#if !UNITY_EDITOR
        SaveImage(path);
#endif
        LogManager.Log($"保存图片到相册，{path}");
    }

    public void SaveVideoToAlbum(string path)
    {
#if !UNITY_EDITOR
        SaveVideo(path);
#endif
        LogManager.Log($"保存视频到相册，{path}");
    }

    public void Clear() { }
}
#endif